package com.loklok.confuse.ui;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.loklok.confuse.extension.PathConfigSettingCache;
import com.loklok.confuse.model.CodePathConfig;
import com.loklok.confuse.model.ConfuseConfig;
import com.loklok.confuse.util.CommonUtil;
import com.loklok.confuse.util.LocalFileUtils;
import com.loklok.confuse.util.OpenAIUtil;
import com.loklok.confuse.util.VelocityUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public class GenerateRaisedCodeDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextArea methodPathText;
    private JTextField packageName;

    public GenerateRaisedCodeDialog(AnActionEvent anActionEvent) {
        setContentPane(contentPane);
        setModal(true);
        setResizable(true);
        setSize(800, 500);
        setLocation(600, 200);
        getRootPane().setDefaultButton(buttonOK);

        PathConfigSettingCache state = PathConfigSettingCache.getInstance(anActionEvent.getProject());
        Project project = anActionEvent.getProject();
        if (state == null || state.getConfig() == null || state.getConfig().requiredFieldsEmpty()) {
            CommonUtil.configureMessageReminders(project);
            return;

        }
        CodePathConfig config = state.getConfig();
        List<ConfuseConfig.LoadRaisedCodeConfig> loadCodeConfigs = config.loadRaisedCodeConfigObj();

        buttonOK.addActionListener(e -> {
            buttonOK.setEnabled(false);
            String methodPath = methodPathText.getText();
            String name = packageName.getText();
            if (StringUtils.isEmpty(methodPath) || StringUtils.isEmpty(name)) {
                buttonOK.setEnabled(true);
                Messages.showMessageDialog("接口url或者包名不能为空!", "提示", Messages.getWarningIcon());
                return;
            }


            try {
                List<ConfuseConfig.LoadCodeConfigItem> items = new ArrayList<>();
                loadCodeConfigs.stream().filter(it -> methodPath.contains(it.getUrl())).forEach(item -> item.getItems().stream()
                        .forEach(configItem -> configItem.getClassNames().stream().forEach(classNameItem -> {
                            ConfuseConfig.LoadCodeConfigItem loadCodeConfigItem = new ConfuseConfig.LoadCodeConfigItem();
                            loadCodeConfigItem.setType(configItem.getType());
                            loadCodeConfigItem.setClassName(classNameItem);
                            loadCodeConfigItem.setLoadPath(configItem.getLoadPath());
                            loadCodeConfigItem.setMethods(configItem.getMethods());
                            items.add(loadCodeConfigItem);
                        })));

                LocalFileUtils.backgroundGenerateCode(anActionEvent, project, config, name, items);
            } catch (Exception ex) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog("An error occurred: " + ex.getMessage(), "Error", Messages.getErrorIcon()));
                return;
            }
            onOK();
        });
    }


    private void onOK() {
        // add your code here
        dispose();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        buttonOK = new JButton();
        buttonOK.setText("OK");
        panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel3.setBorder(BorderFactory.createTitledBorder(null, "请输入需要生成的接口Url,多个逗号隔开", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        methodPathText = new JTextArea();
        panel3.add(methodPathText, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel4, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel4.setBorder(BorderFactory.createTitledBorder(null, "包名称配置", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JLabel label1 = new JLabel();
        label1.setText("输入包名");
        panel4.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        packageName = new JTextField();
        panel4.add(packageName, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
